package com.rjkj.medicalwaste.ui.xlb

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.rjkj.medicalwaste.R
import com.rjkj.medicalwaste.adapter.BoxListAdapter
import com.rjkj.medicalwaste.adapter.ItemClick
import com.rjkj.medicalwaste.adapter.UserListAdapter
import com.rjkj.medicalwaste.base.BaseFragment
import com.rjkj.medicalwaste.bean.CodeType
import com.rjkj.medicalwaste.databinding.FragmentAllUserDataBinding
import com.rjkj.medicalwaste.viewmodels.BluetoothControlViewModel
import com.rjkj.medicalwaste.viewmodels.provider.MainViewModelProvider
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch


class AllUserDataFragment : BaseFragment<FragmentAllUserDataBinding>() {
    val viewModel:AllUserDataViewModel by viewModels{
        MainViewModelProvider.providerAllUserModel(requireContext())
    }

    @ExperimentalCoroutinesApi
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = FragmentAllUserDataBinding.inflate(layoutInflater, container, false)
        binding.lifecycleOwner = viewLifecycleOwner
        onSubscribeUi(binding)
        return binding.root
    }

    @ExperimentalCoroutinesApi
    override fun onSubscribeUi(binding: FragmentAllUserDataBinding) {
        val userListAdapter = UserListAdapter()
        binding.list.apply {
            val dividerItemDecoration = DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
            addItemDecoration(dividerItemDecoration)
            layoutManager = LinearLayoutManager(requireContext())
            adapter = userListAdapter
            initSwipeToDelete(this)

        }
        lifecycleScope.launch {
            viewModel.users.collectLatest {
                userListAdapter.submitData(it)
            }
        }
        binding.spinner.layoutManager = LinearLayoutManager(
            requireContext(),
            LinearLayoutManager.HORIZONTAL,
            false
        )
        binding.spinner.adapter = SpinnerAdapter(CodeType.values().toList(),
            object : ItemClick<CodeType> {
                override fun onClicked(view: View, value: CodeType) {
                    viewModel.setType(value)
                }
            })

    }
    private fun initSwipeToDelete(recyclerView: RecyclerView) {

        /**
         * 位于 [androidx.recyclerview.widget] 包下，已经封装好的控件
         */
        ItemTouchHelper(object : ItemTouchHelper.Callback() {
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int =
                makeMovementFlags(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)

            override fun onMove(
                recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean = false

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {

            }
        }).attachToRecyclerView(recyclerView)
    }



    class SpinnerAdapter(
        private val list: List<CodeType>,
        private val itemClick: ItemClick<CodeType>
    ):RecyclerView.Adapter<SpinnerAdapter.MyViewHolder>(){
        class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            var textView1:TextView = view.findViewById(R.id.text1) as TextView
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            return  MyViewHolder(
                LayoutInflater.from(parent.context).inflate(
                    R.layout.item_hor_button,
                    parent,
                    false
                )
            )
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            list[position].let { type->
                holder.textView1.text = type.alias
                holder.itemView.setOnClickListener{
                    itemClick.onClicked(it, type)
                }
            }

        }

        override fun getItemCount(): Int {
           return list.size
        }

    }
}